const std = @import("std");
const jetzig = @import("jetzig");
const article_service = @import("../../service/article_service.zig");
const cate_service = @import("../../service/cate_service.zig");
const article_model = @import("../../model/article.zig");

/// 某个分类下文章列表
pub fn get(cate_name: []const u8, req: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
    std.debug.print("cate_name: {s}\n", .{cate_name});

    var root = try data.object();

    // 文章列表数据
    const articles = try article_service.getArticlesByCateName(cate_name);

    var arts_array = try data.array();
    for (articles) |art| {
        var art_obj = try data.object();
        try art_obj.put("id", data.integer(art.id));
        try art_obj.put("title", data.string(art.title));
        try art_obj.put("description", data.string(art.description));
        try art_obj.put("content", data.string(art.content));
        try art_obj.put("istop", data.boolean(art.istop == 1));
        try art_obj.put("cate_name", data.string(art.cate_name));
        try art_obj.put("created_at", data.string(art.created_at));

        try arts_array.append(art_obj);
    }
    try root.put("articles", arts_array);
    try root.put("cate_name", data.string(cate_name));

    // 分类列表数据
    const cates = try cate_service.getCategoriesWhitArtCount();
    var cates_array = try data.array();
    for (cates) |cate| {
        var cate_obj = try data.object();
        try cate_obj.put("id", data.integer(cate.id));
        try cate_obj.put("name", data.string(cate.name));
        try cate_obj.put("count", data.integer(cate.count));

        try cates_array.append(cate_obj);
    }
    try root.put("categories", cates_array);

    return req.render(.ok);
}

// pub fn index(request: *jetzig.Request) !jetzig.View {
//     return request.render(.ok);
// }

// pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
//     _ = id;
//     return request.render(.ok);
// }

// test "index" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.GET, "/article/cate", .{});
//     try response.expectStatus(.ok);
// }

// test "get" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.GET, "/article/cate/example-id", .{});
//     try response.expectStatus(.ok);
// }

// test "new" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.GET, "/article/cate/new", .{});
//     try response.expectStatus(.ok);
// }

// test "post" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.POST, "/article/cate", .{});
//     try response.expectStatus(.created);
// }

// test "put" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.PUT, "/article/cate/example-id", .{});
//     try response.expectStatus(.ok);
// }

// test "patch" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.PATCH, "/article/cate/example-id", .{});
//     try response.expectStatus(.ok);
// }

// test "delete" {
//     var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
//     defer app.deinit();

//     const response = try app.request(.DELETE, "/article/cate/example-id", .{});
//     try response.expectStatus(.ok);
// }
